package util;

import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
public class MyMailSender {

	private JavaMailSenderImpl mailSender;
	private String from;

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public JavaMailSenderImpl getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSenderImpl mailSender) {
		this.mailSender = mailSender;
	}

	public void sendEmail(String accept, String title, String content) {
		MimeMessage mailMessage = mailSender.createMimeMessage();
		try {
			// 设置utf-8或GBK编码，否则邮件会有乱码
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
			messageHelper.setTo(accept);// 接受者
			messageHelper.setFrom(this.getFrom());// 发送者
			messageHelper.setSubject(title);// 主题
			// 邮件内容，注意加参数true，表示启用html格式
			messageHelper.setText(content, true);
			mailSender.send(mailMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
